// UpdateProduce.cpp

#include "StdAfx.h"

#include "UpdateProduce.h"

namespace Common
{
namespace Archive
{

static const char *kUpdateActionSetCollision = "Internal collision in update action set";

void UpdateProduce(
    const std::vector<CUpdatePair> &updatePairs,
    const Update::CActionSet &actionSet,
	std::vector<CUpdatePair2> &operationChain,
    IUpdateProduceCallback *callback)
{
  for (size_t i = 0; i < updatePairs.size(); i++)
  {
    const CUpdatePair &pair = updatePairs[i];

    CUpdatePair2 up2;
    up2.IsAnti = false;
    up2.DirIndex = pair.DirIndex;
    up2.ArcIndex = pair.ArcIndex;
    up2.NewData = up2.NewProps = true;
    
    switch(actionSet.StateActions[static_cast<int>(pair.State)])
    {
	case Update::PairAction::kIgnore:
        /*
        if (pair.State != PairState::kOnlyOnDisk)
          IgnoreArchiveItem(m_ArchiveItems[pair.ArcIndex]);
        // cout << "deleting";
        */
        if (callback)
          callback->ShowDeleteFile(pair.ArcIndex);
        continue;

	case Update::PairAction::kCopy:
        if (pair.State == Update::PairState::State::kOnlyOnDisk)
          throw kUpdateActionSetCollision;
        up2.NewData = up2.NewProps = false;
        break;
      
      case Update::PairAction::kCompress:
        if (pair.State == Update::PairState::State::kOnlyInArchive ||
            pair.State == Update::PairState::State::kNotMasked)
          throw kUpdateActionSetCollision;
        break;
      
      case Update::PairAction::kCompressAsAnti:
        up2.IsAnti = true;
        break;
    }
    operationChain.push_back(up2);
  }
  operationChain.shrink_to_fit();
}

}
}